/*
 * Copyright (C) 2014 Niklas Baudy <http://vanniktech.de/Imprint>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ywwxhz.lib;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TimePicker;

public class TimePickerPreference extends DialogPreference {

    private String mSelectedValue;
    private TimePicker mTimePicker;

    public TimePickerPreference(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSetInitialValue(final boolean restoreValue, final Object defaultValue) {
        mSelectedValue = restoreValue ? this.getPersistedString("00:00") : (String) defaultValue;
        this.updateSummary();
    }

    @Override
    protected Object onGetDefaultValue(final TypedArray a, final int index) {
        return a.getString(index);
    }

    @Override
    protected void onPrepareDialogBuilder(final Builder builder) {
        super.onPrepareDialogBuilder(builder);
//        if(mSelectedValue==null){
//            mSelectedValue = "00:00";
//        }
        String[] time = mSelectedValue.split(":");
        mTimePicker = new TimePicker(this.getContext());
        mTimePicker.setIs24HourView(true);
        mTimePicker.setCurrentHour(Integer.parseInt(time[0]));
        mTimePicker.setCurrentMinute(Integer.parseInt(time[1]));
        mTimePicker.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        final LinearLayout linearLayout = new LinearLayout(this.getContext());
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.addView(mTimePicker);

        builder.setView(linearLayout);
    }

    @Override
    protected void onDialogClosed(final boolean positiveResult) {
        if (positiveResult && this.shouldPersist()) {
            mSelectedValue = pauseIntToString(mTimePicker.getCurrentHour()) + ":" + pauseIntToString(mTimePicker.getCurrentMinute());
            this.persistString(mSelectedValue);
            this.updateSummary();
        }
    }

    private String pauseIntToString(int val) {
        if (val < 10) {
            return "0" + val;
        } else {
            return "" + val;
        }
    }

    private void updateSummary() {
        super.setSummary(super.getTitle() + " " + mSelectedValue);
    }
}